import { OB11Group } from '@/napcat-onebot/index';
import { OB11Construct } from '@/napcat-onebot/helper/data';
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { ActionName } from '@/napcat-onebot/action/router';
import { Static, Type } from '@sinclair/typebox';

const SchemaData = Type.Object({
  group_id: Type.Union([Type.Number(), Type.String()]),
});

type Payload = Static<typeof SchemaData>;

class GetGroupInfo extends OneBotAction<Payload, OB11Group> {
  override actionName = ActionName.GetGroupInfo;
  override payloadSchema = SchemaData;

  async _handle (payload: Payload) {
    const group = (await this.core.apis.GroupApi.getGroups()).find(e => e.groupCode === payload.group_id.toString());
    if (!group) {
      const data = await this.core.apis.GroupApi.fetchGroupDetail(payload.group_id.toString());
      if (data.ownerUid && data.ownerUin === '0') {
        data.ownerUin = await this.core.apis.UserApi.getUinByUidV2(data.ownerUid);
      }
      return {
        ...data,
        group_all_shut: data.shutUpAllTimestamp > 0 ? -1 : 0,
        group_remark: '',
        group_id: +payload.group_id,
        group_name: data.groupName,
        member_count: data.memberNum,
        max_member_count: data.maxMemberNum,
      };
    }
    return OB11Construct.group(group);
  }
}

export default GetGroupInfo;
